package com.suning.sawp.web.controller;

import java.security.Principal;
import java.util.regex.Pattern;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.suning.sawp.constants.ErrorCodeConstants;
import com.suning.sawp.dto.caller.CallerInfo;
import com.suning.sawp.dto.common.ReturnMsg;
import com.suning.sawp.intf.caller.CallerService;
import com.suning.sawp.web.exception.MyGsonView;

@Controller
@RequestMapping("/callerInfo")
public class CallerController extends DjBaseController {

	private static final Logger LOGGER = LoggerFactory.getLogger(ClerkController.class);

	private static final String MOBILE_REGEX = "^[1][3,4,5,8][0-9]{9}$";

	@Resource
	CallerService callerService;

	/**
	 * 获取店员邀请码
	 * 
	 * @param principal
	 * @return
	 */
	@RequestMapping("/query.do")
	public MyGsonView queryCallerInfo(@RequestParam(value = "phoneNo", required = false) String phoneNo,
			Principal principal) {
		LOGGER.debug("查询店员来电显示信息,staffId:{},phoneNo：{}", principal.getName(), phoneNo);
		ReturnMsg<CallerInfo> ret = new ReturnMsg<CallerInfo>();
		if (StringUtils.isBlank(phoneNo) || !Pattern.matches(MOBILE_REGEX,phoneNo)) {
			ret.setRetFlag(ReturnMsg.FAIL);
			ret.setError(ErrorCodeConstants.ERR_0001);
			ret.setErrorMessage(ErrorCodeConstants.ERR_MAP.get(ErrorCodeConstants.ERR_0001));
			return setGsonView(ret);
		}
		String staffId = principal.getName();
		try {
			CallerInfo callerInfo = callerService.queryCallerInfo(staffId, phoneNo);
			ret.setData(callerInfo);
		} catch (Exception e) {
			ret.setRetFlag(ReturnMsg.FAIL);
			ret.setErrorCode(ErrorCodeConstants.ERR_0002);
			ret.setErrorMessage(e.getMessage());
			LOGGER.error("查询店员来电显示信息异常，staffId+phoneNo:{}", principal.getName() + phoneNo, e);
		}
		return setGsonView(ret);
	}

}
